﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using TinyLib.Input;
using TinyLib.Sprite;
using TinyLib.Entity;

namespace GodBuilderIdea
{
    public class Button : Entity
    {
        public enum ButtonType
        {
            PAINT,
            BUCKET,
            MOVE,
            NEXT
        }
        private bool m_enabled = false;
        public bool m_pressed = false;
        public ButtonType m_type;
        SpriteFont font;
        String m_quick_btn = "";
        public int m_cost = 0;

        
        public Button(Game game, SpriteBatch sprite_batch, ButtonType type)
            : base(game, sprite_batch)
        {
            m_type = type;
            Initialize();
        }

        public void Enable()
        {
            Enabled = true;

            if (m_pressed)
            {
                this.Color = Color.Magenta;
            }
            else
            {
                this.Color = Color.White;
            }              
        }

        public void Disable()
        {
            Enabled = false;
            this.Color = Color.White;
        }

        public override void Initialize()
        {
            if (m_type == ButtonType.PAINT)
            { texture2D = Game.Content.Load<Texture2D>("images/painting_tool_btn"); m_quick_btn = "A"; m_cost = 1; }
            else if (m_type == ButtonType.BUCKET)
            { texture2D = Game.Content.Load<Texture2D>("images/bucket_tool_btn"); m_quick_btn = "S"; m_cost = 5; }
            else if (m_type == ButtonType.MOVE)
            { texture2D = Game.Content.Load<Texture2D>("images/move_tribe_btn"); m_quick_btn = "D"; m_cost = 5; }
            else if (m_type == ButtonType.NEXT)
            { texture2D = Game.Content.Load<Texture2D>("images/done_btn"); }
            
            AnimatedSprite = new AnimatedSprite(Spritebatch, texture2D, Game);
            AnimatedSprite.FrameSize = new Vector2(1, 2);
            AnimatedSprite.AddStrip("Idle", 1, 1000);
            AnimatedSprite.AddStrip("Hover", 1, 1000);
            AnimatedSprite.position = new Vector2(1400, 565);
            AnimatedSprite.Animation = "Idle";

            font = Game.Content.Load<SpriteFont>("LargeFont");

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            MouseState mstate = Mouse.GetState();
           
            if (Rectangle.Contains(new Point(mstate.X, mstate.Y)) || m_pressed)
            { AnimatedSprite.Animation = "Hover"; this.Color = Color.Yellow; }
            else
            { AnimatedSprite.Animation = "Idle"; this.Color = Color.White; }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (!Enabled)
                return;

            base.Draw(gameTime);

            Spritebatch.DrawString(font, m_quick_btn, new Vector2((int)AnimatedSprite.position.X+15, (int)AnimatedSprite.position.Y+55), this.Color);
            if (m_cost > 0)
            { Spritebatch.DrawString(font, "[" + m_cost + "]", new Vector2((int)AnimatedSprite.position.X - 10, (int)AnimatedSprite.position.Y + 95), this.Color); }
        }
    }
}
